package com.xtaifghy.wugui;

import java.util.Random;
import java.util.ArrayList;

public class GameState4 implements GameStateInterface {
    private boolean [] state = {true,true,true,true,true,true,true,true};
    private boolean [] goalState = {false,false,false,true,true,true,true,true};
    Random rand = new Random();
    private static final int[][] actions={
            {0,1,3},//1
            {0,1,2},//2
            {1,2,4},//3
            {0,5,3},//8
            {1,4,6,3},//0
            {2,4,7},//4
            {6,5,3},//7
            {6,5,7},//6
            {6,4,7},//5
    };

    public GameState4() {
        randomGoalState();
    }

    /*
     *更改多盏灯的状态
     * @param lArr: 存储需要改变状态的灯的序号
     */
    public void changeLights(int [] lArr){
        for (int i:lArr) {
            state[i]=!state[i];
        }
    }

    @Override
    public void doAction(int a){
        changeLights(actions[a]);
    }

    /*
     *判断当前状态是否为目标状态
     */
    @Override
    public boolean testState(){
        boolean flag = true;
        for (int i = 0; i < state.length; i++) {
            if(state[i]!=goalState[i]) flag = false;
        }
        return flag;
    }

    @Override
    public ArrayList<boolean[]> getPaths(){
        ArrayList<boolean[]> paths = new ArrayList<>();
        boolean[] path = new boolean[9];
        path[1] = true;
        generatePath(path);
        paths.add(path.clone());
        path[1] = false;
        generatePath(path);
        paths.add(path);
        return paths;
    }

    private void generatePath(boolean[] path){
        path[2]=state[0]^goalState[0]^state[2]^goalState[2]^state[3]^goalState[3]^state[6]^goalState[6]^state[7]^goalState[7];
        path[0]=state[0]^goalState[0]^state[2]^goalState[2]^state[4]^goalState[4]^state[5]^goalState[5]^state[6]^goalState[6];
        path[4]=path[0]^path[1]^path[2]^state[1]^goalState[1];
        path[3]=path[0]^path[1]^state[0]^goalState[0];
        path[5]=path[1]^path[2]^state[2]^goalState[2];
        path[6]=path[0]^path[3]^path[4]^state[3]^goalState[3];
        path[7]=path[2]^path[4]^state[4]^goalState[4]^state[7]^goalState[7];
        path[8]=path[5]^path[7]^state[7]^goalState[7];
    }

    @Override
    public void setState(boolean[] state) {
        this.state = state;
    }

    @Override
    public boolean[] getState() {
        return state.clone();
    }

    @Override
    public void setGoalState(boolean[] goalState) {
        this.goalState = goalState;
    }

    @Override
    public boolean[] getGoalState() {
        return goalState.clone();
    }

    @Override
    public void randomGoalState(){
        for (int i = 0; i < goalState.length; i++) {
            goalState[i] = rand.nextBoolean();
        }
    }
}
